include .env
export $(shell sed 's/=.*//' .env)
GRPC_GATEWAY_DIR := $(shell go list -f '{{ .Dir }}' -m github.com/grpc-ecosystem/grpc-gateway 2> /dev/null)
GRPC_GEN_DOC_DIR := $(shell which protoc-gen-doc)
GO_INSTALLED := $(shell which go)
PROTOC_INSTALLED := $(shell which protoc)
BINDATA_INSTALLED := $(shell which go-bindata 2> /dev/null)
PGGG_INSTALLED := $(shell which protoc-gen-grpc-gateway 2> /dev/null)
PGG_INSTALLED := $(shell which protoc-gen-go 2> /dev/null)
PGD_INSTALLED := $(shell which protoc-gen-doc 2> /dev/null)

# Go parameters
GOCMD=GO111MODULE=on go
GOBUILD=$(GOCMD) build
GODOWNLOAD=$(GOCMD) mod download


install-tools:
ifndef PROTOC_INSTALLED
	$(error "go is not installed, please run 'brew install go'")
endif
ifndef PROTOC_INSTALLED
	$(error "protoc is not installed, please run 'brew install protobuf'")
endif
ifndef BINDATA_INSTALLED
	@go get -u github.com/kevinburke/go-bindata/go-bindata@master
endif
ifndef PGGG_INSTALLED
	@go get -u github.com/grpc-ecosystem/grpc-gateway/...
endif
ifndef PGG_INSTALLED
	@go get -u github.com/golang/protobuf/protoc-gen-go
endif
ifndef PGD_INSTALLED
	@go get -u github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc
endif


generate: install-tools
	@protoc \
		-I/usr/local/include \
		-I=$(GOPATH)/src \
		-I. \
		-I$(GRPC_GATEWAY_DIR)/third_party/googleapis \
		--go_out=plugins=grpc:api \
		--grpc-gateway_out=logtostderr=true:api \
		--plugin=protoc-gen-doc=$(GRPC_GEN_DOC_DIR) \
		--doc_opt=html,index.html \
		--doc_out=./doc \
		--proto_path=./api/proto/v1 \
		health.proto \
		image.proto \
		notify.proto \
		profit.proto \
		refund.proto \
		service.proto \
		sub_mch.proto \
		trade.proto \
		transfer.proto

gen:
	protoc \
    -I${GOPATH}/src -I./api/proto/v1 -I. \
    --gofast_out=plugins=grpc,\Mgoogle/protobuf/struct.proto=github.com/gogo/protobuf/types:pkg/api/v1 \
    --govalidators_out=pkg/api/v1 \
    --grpc-gateway_out=allow_delete_body=true,allow_patch_feature=false,logtostderr=true:./pkg/api/v1/ \
    api/proto/v1/*.proto 

model:
	gen --sqltype=postgres --connstr "$(DB_CONN_STR)" --database wxpay -t refund  --out ./tmp --module model --gorm  --json-fmt=snake --overwrite  

download:
	$(GODOWNLOAD)

build:
	rm -rf target
	mkdir target
	cp cmd/config-example.toml target/config.toml
	$(GOBUILD) -o target/wxpay cmd/server.go

clean:
	rm -rf target

dev:
	go run cmd/server.go -c=cmd/config-example.toml

run:
    # 启动 wxpay 应用
	target/wxpay -c=target/config.toml

run2:
    # 启动 wxpay 应用
	nohup target/wxpay -c=target/config.toml 2>&1 > target/wxpay.log &

stop:
    # 关闭 wxpay 应用
	pkill -f target/wxpay
